<?php
class Ecore_Mail extends Zend_Mail
{
	var $mail 	   = null;
	var $transport = null;

	public function __construct()
	{
		$objConf = new Zend_Config_Ini(ROOT_DIR . '/configs/config.ini', 'smtp');
		$arrConf = $objConf->smtp->toArray();
		$this->transport = new Zend_Mail_Transport_Smtp($arrConf['host'], $arrConf['config']);
		$this->mail 	 = new Zend_Mail('UTF-8');
	}

	public function buildHtml()
	{
		$this->setType(Zend_Mime::MULTIPART_RELATED);
		$strBody = $this->mail->getBodyHtml(true);
		if (function_exists('tidy_repair_string')) {
			$strBody = tidy_repair_string($strBody, array(
			'output-xhtml' 					=> true,
			'show-body-only' 				=> true,
			'doctype' 						=> 'strict',
			'drop-font-tags' 				=> true,
			'drop-proprietary-attributes' 	=> true,
			'lower-literals' 				=> true,
			'quote-ampersand' 				=> true,
			'wrap' => 0));
		}
		$dom = new DOMDocument(null, 'UTF-8');
		$dom->loadHTML($strBody);
		$arrImages = $dom->getElementsByTagName('img');
		for ($i = 0; $i < $arrImages->length; $i++) {
			$objImg 	  			= $arrImages->item($i);
			$strUrl 				= $objImg->getAttribute('src');
			$strExt 				= strtolower(strrchr($strUrl, '.'));
			$objMime 				= new Ecore_Mime_Part(file_get_contents($strUrl));
			$objMime->location 		= $strUrl;
			$objMime->type        	= $this->mimeByExtension($strExt);
			$objMime->disposition 	= Zend_Mime::DISPOSITION_INLINE;
			$objMime->encoding    	= Zend_Mime::ENCODING_BASE64;
			$objMime->filename   	= md5($strUrl) . $strExt;
			$this->mail->addAttachment($objMime);
			$this->mail->setBodyHtml($strBody, 'UTF-8', Zend_Mime::ENCODING_8BIT);
		}
	}

	public function mimeByExtension($strExt)
	{
		switch ($strExt) {
			case '.gif':
				$strType = 'image/gif';
				break;
			case '.jpg':
			case '.jpeg':
				$strType = 'image/jpg';
				break;
			case '.png':
				$strType = 'image/png';
				break;
			default:
				$strType = 'application/octet-stream';
		}
		return $strType;
	}

	/**
	* Name: Ecore_Mail <br>
	* @input string strFrom		: Email nguioi gui
	* @input string strFromName	: Ten nguioi gui
	* @input string strTo		: Email nguoi nhan
	* @input string strSubject	: Tieu de mail
	* @input string strBody		: Noi dung mail
	* @input string strCC		: Email CC
	* @input string strBCC		: Email BCC
	* @output 1/0
	*/
	public function send($strFrom, $strFromName, $strTo, $strSubject = '', $strBody = '', $strCC = '', $strBCC = '')
	{
		try {
			if ($strFrom != '' && $strFromName != '' && $strTo != '' && $strSubject != '' && $strBody != '') {
				$objValidateEmail = new Zend_Validate_EmailAddress();
				
				$this->mail->setFrom($strFrom, $strFromName);
				$arrTo = is_array($strTo) ? $strTo : explode(';', str_replace(',', ';', $strTo));
				for ($i = 0; $i < count($arrTo); $i++) {
					if ($objValidateEmail->isValid($arrTo[$i])) {
						$this->mail->addTo($arrTo[$i]);
					}
				}
				$this->mail->setSubject($strSubject);
				$this->mail->setBodyHtml($strBody, 'UTF-8', Zend_Mime::MULTIPART_RELATED);
				$this->buildHtml();
				return $this->mail->send($this->transport);
			}
		} catch (Exception $exception) {
			print_r($exception);exit();
		}
		return 0;
	}
	
	public function isValidEmail($arrEmail = array()) 
	{		
		if ($arrEmail) {
			$objValidateEmail = new Zend_Validate_EmailAddress();
			for ($i = 0; $i < count($arrEmail); $i++) {
				if (!$objValidateEmail->isValid($arrEmail[$i])) {
					return 0;
				}
			}
		}
		return 1;
	}
}
?>